import 'package:extended_nested_scroll_view/extended_nested_scroll_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter_study_app/app/router/router_param.dart';
import 'package:flutter_study_app/app/router/routers.dart';
import 'package:flutter_study_app/module/online_lend/page/post_own_lend_page.dart';
import 'package:flutter_study_app/module/online_lend/page/post_salary_lend_page.dart';
import 'package:flutter_study_app/module/statistics/widget/common_sliver_header_delegate.dart';
import 'package:flutter_study_app/module/statistics/widget/my_underline_tab_indicator.dart';
import 'package:flutter_study_app/module/statistics/widget/sticky_tab_bar_delegate.dart';
import 'package:flutter_study_app/widget/my_app_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class OnlineLendWhiteListPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _OnlineLendWhiteListPageState();
}

class _OnlineLendWhiteListPageState extends State<OnlineLendWhiteListPage>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;

  String _clientName = "";
  String _clientType = "全部";

  String _companyName = "";
  String _companyType = "全部";

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 2, vsync: this);
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  _buildDataItemView(
    String title,
    String value,
    String num1,
    String num2,
  ) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 12.w),
      decoration: BoxDecoration(
        color: Color(0xFFFFFFFF),
        borderRadius: BorderRadius.circular(8.w),
      ),
      child: Column(
        children: [
          Text(
            title,
            style: TextStyle(color: Color(0xFF666666), fontSize: 14.sp),
          ),
          Expanded(
              child: Text(
            value,
            style: TextStyle(
                color: Color(0xFF333333),
                fontSize: 24.sp,
                fontWeight: FontWeight.bold),
          )),
          Container(
            height: 20.w,
            padding: EdgeInsets.symmetric(horizontal: 4.w),
            decoration: BoxDecoration(
                color: Color(0xFFFFF1D9),
                borderRadius: BorderRadius.circular(2.w)),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Expanded(
                  child: Text('邮薪贷',
                      style:
                          TextStyle(color: Color(0xFFF29127), fontSize: 11.sp)),
                ),
                Expanded(
                  child: Text(num1,
                      textAlign: TextAlign.center,
                      style:
                          TextStyle(color: Color(0xFFF29127), fontSize: 11.sp)),
                ),
              ],
            ),
          ),
          SizedBox(
            height: 6.w,
          ),
          Container(
            height: 20.w,
            padding: EdgeInsets.symmetric(horizontal: 4.w),
            decoration: BoxDecoration(
                color: Color(0xFFEAF4FF),
                borderRadius: BorderRadius.circular(2.w)),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Expanded(
                  child: Text('邮享贷',
                      style:
                          TextStyle(color: Color(0xFF1B96FF), fontSize: 11.sp)),
                ),
                Expanded(
                  child: Text(num2,
                      textAlign: TextAlign.center,
                      style:
                          TextStyle(color: Color(0xFF1B96FF), fontSize: 11.sp)),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    final double statusBarHeight = MediaQuery.of(context).padding.top;

    return Scaffold(
      appBar: MyAppBar(
        title: '网贷白名单专区',
        showDivider: true,
        rightMenu: InkWell(
          onTap: () {
            int type = _tabController.index;
            RouterParam param = RouterParam();
            param.putInt("type", _tabController.index);
            param.putString("name", type == 0 ? _clientName : _companyName);
            param.putString(
                "search_type", type == 0 ? _clientType : _companyType);

            Routers.navigateToResult(context, Routers.onlineLendSearchPage,
                (data) {
              if (data != null && data is Map) {
                if (type == 0) {
                  _clientName = data['name'] ?? '';
                  _clientType = data['type'] ?? '全部';
                } else {
                  _companyName = data['name'] ?? '';
                  _companyType = data['type'] ?? '全部';
                }
              }
            }, parameters: param);
          },
          child: Container(
            padding: EdgeInsets.only(left: 24.w, right: 12.w),
            child: Image.asset(
              'assets/images/ic_search.png',
              width: 24.w,
              height: 24.w,
            ),
          ),
        ),
      ),
      body: ExtendedNestedScrollView(
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
          return <Widget>[
            SliverPersistentHeader(
              delegate: CommonSliverHeaderDelegate(
                isLucency: true,
                child: PreferredSize(
                  preferredSize: Size.fromHeight(150.w),
                  child: Container(
                    color: Color(0xFFF5F4F3),
                    padding: EdgeInsets.all(12.w),
                    child: Row(
                      children: [
                        Expanded(
                            child: _buildDataItemView('客户总数', '99', '9', '90')),
                        SizedBox(
                          width: 12.w,
                        ),
                        Expanded(
                            child: _buildDataItemView(
                                '累计联系', '123,456', '123,456', '122')),
                        SizedBox(
                          width: 12.w,
                        ),
                        Expanded(
                            child: _buildDataItemView('本周联系', '4', '1', '3')),
                      ],
                    ),
                  ),
                ),
              ),
            ),
            SliverPersistentHeader(
                pinned: true,
                floating: true,
                delegate: StickyTabBarDelegate(
                  height: 42.w,
                  child: TabBar(
                    labelPadding: EdgeInsets.zero,
                    indicatorColor: Color(0xff009669),
                    indicatorPadding: EdgeInsets.zero,
                    controller: _tabController,
                    labelColor: Color(0xff009669),
                    labelStyle: TextStyle(fontSize: 14.sp),
                    unselectedLabelColor: Color(0xff333333),
                    unselectedLabelStyle: TextStyle(fontSize: 14.sp),
                    indicator: MyUnderlineTabIndicator(
                        borderSide:
                            BorderSide(width: 2.w, color: Color(0xff009669)),
                        needWidth: 70.w),
                    tabs: [
                      Tab(text: '邮薪贷'),
                      Tab(text: '邮享贷'),
                    ],
                  ),
                )),
          ];
        },
        pinnedHeaderSliverHeightBuilder: () {
          return statusBarHeight + 21.w;
        },
        onlyOneScrollInBody: false,
        body: SafeArea(
          child: TabBarView(
            physics: BouncingScrollPhysics(),
            controller: _tabController,
            children: [
              PostSalaryLendPage(),
              PostOwnLendPage(),
            ],
          ),
        ),
      ),
    );
  }
}
